package thread;

import entity.User;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceDemo {

    public static void main(String[] args) {
        User user1 = new User("Jack",25);
        User user2 = new User("Tom",21);
        User user3 = new User("Ros",28);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);
        //CAS操作 主内存中的原始值user1和期望值user1比较相等,返回值为true且将主内存中的原始值修改为user2;
        System.out.println(atomicReference.compareAndSet(user1,user2)+"\t"+atomicReference.get());
        //CAS操作 主内存中的原始值user2和期望值user1比较不相等,返回值为false,不更新期望值;
        System.out.println(atomicReference.compareAndSet(user1,user3)+"\t"+atomicReference.get());
        //true	User(username=Tom, age=21)
        //false	User(username=Tom, age=21)
    }
}
